package org.alex.samples.dao;

import java.util.ArrayList;
import java.util.List;

import org.alex.samples.model.ExamScore;
import org.alex.samples.model.ExamUserSubject;
import org.alex.samples.model.Subject;
import org.alex.samples.vo.SubjectVO;
import org.alex.samples.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class ExamUserSubjectDAOImpl extends BaseHibernateDAO<ExamUserSubject> implements IExamUserSubjectDAO{
	
	@Autowired
	IExamScoreDAO examScoreDAO;
	
	public List<ExamUserSubject> FindAllExamUserSubject(){
		String hql = " FROM ExamUserSubject ";
		return queryForList(hql, null);
//		List<ExamUserSubject> list = this.;
//		return list;
	}

	@Override
	public List<UserVO> FindAllUserByS(Integer subjectId) {
		return null;
	}

	@Override
	public List<SubjectVO> FindAllSubjectByU(Integer userId) {
		String hql=" FROM ExamUserSubject where user_id = ? ";
		List<ExamUserSubject> allList = queryForList(hql, new Object[]{userId});
		List<SubjectVO> list = new ArrayList<SubjectVO>();
		for(ExamUserSubject eus:allList){
			Subject subject = eus.getSubject();
			SubjectVO subjectvo = new SubjectVO();
			subjectvo.setCount(subject.getCount());
			subjectvo.setDescription(subject.getDescription());
			subjectvo.setId(subject.getId());
			subjectvo.setName(subject.getName());
			subjectvo.setScore(subject.getScore());
			subjectvo.setStatus(subject.isStatus());
			subjectvo.setTime(subject.getTime());
			ExamScore examScore = examScoreDAO.getScoreByUIdSId(userId,subject.getId());
			subjectvo.setTestscore(examScore.getScore());
//			Date time = examScore.getExamTime();
//			Calendar calendar = Calendar.getInstance();
//			calendar.setTime(time);
//			time = calendar.getTime();
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
//			subjectvo.setExamTime(sdf.format(time));
			
			list.add(subjectvo);
		}
		return list;
	}

	@Override
	public SubjectVO FindSubjectByUserIdSubjectId(Integer id, Integer subjectId) {
		return null;
	}

//	@Override
//	public List<User> FindAllUserByS(Subject subject) {
//		String hql=" FROM ExamUserSubject where sub_id = ? ";
//		List<ExamUserSubject> allList = queryForList(hql, new Object[]{subject.getId()});
//		List<User> list = new ArrayList<User>();
//		for(ExamUserSubject examUserSubject:allList){
//			list.add(examUserSubject.getUser());
//		}
//		return list;
//	}
//
//	@Override
//	public List<Subject> FindAllSubjectByU(User user) {
//		String hql=" FROM ExamUserSubject where user_id = ? ";
//		List<ExamUserSubject> allList = queryForList(hql, new Object[]{user.getId()});
//		List<Subject> list = new ArrayList<Subject>();
//		for(ExamUserSubject examUserSubject:allList){
//			list.add(examUserSubject.getSubject());
//		}
//		return list;
//	}
	
	
}
